##--------------------------------------------------##
## Project: End of Currency Manipulation?                  
## Author: Weldzius, Ryan                           
## Journal: Economics & Politics    
## Upate: October 2020                      
##--------------------------------------------------##

##--------------------------------##
## 0. Preliminaries---------------##
##--------------------------------##

# clear workspace
rm(list = ls())

# master data:
d <- read.csv(file="currencymanip2020.csv", header=TRUE, sep=",")

#-----------------------------------------------------------------------------------------------------------------------------------------------------------------
# 1. RESTRICT SAMPLE
#-----------------------------------------------------------------------------------------------------------------------------------------------------------------

# sub sample:
df.sub <- d[which(d$ez12==0 & d$ez19==0 & d$notmfg==0 & d$subsample!=1 & d$year>=2000),] # subsample: no EA12/19 countries, no oil exporters or capital centers

# factors, assign variables:
df.sub$year_factor    <- as.factor(df.sub$year) # for year fixed effects
df.sub$country_factor <- as.factor(df.sub$country_cd) # for country fixed effects
df.sub$GPN <- df.sub$l_gvc_exp
df.sub$FVA <- df.sub$l_fva_exp
df.sub$DVX <- df.sub$l_dvx_exp
df.sub$EXP <- df.sub$l_exp_goods_gdp
df.sub     <- subset(df.sub, !is.na(EXP)) 

#-----------------------------------------------------------------------------------------------------------------------------------------------------------------
# 2. SCATTER PLOTS OF MAIN VARIABLES OF INTEREST
#-----------------------------------------------------------------------------------------------------------------------------------------------------------------

df.plot <- d[which(d$ez12==0 & d$ez19==0 & d$notmfg==0 & d$subsample!=1 & d$year>=2000 & d$gdppc_dum==1),]

# FIGURE 1. scatter plot of (forex flows)/(gdp*.02) and current account
df.plot$FX <- df.plot$forex_2pctgdp
df.plot$CA <- df.plot$curacct_gdp
df.plot %>% 
  mutate(quadrant = case_when(FX > 0   & CA >  3  ~ "Q1",
                              TRUE  ~ "Q2"))  %>%
  ggplot(aes(x=FX, y=CA, color=quadrant, shape=quadrant, alpha=quadrant, size=quadrant)) +
  geom_point() + 
  geom_vline(xintercept=0, linetype="dashed", color="black") + 
  geom_hline(yintercept=3, linetype="dashed", color="black") +
  annotate("text",x=-12.5, y=23, hjust=0, label="[Q1]", size=6, color="black") +
  annotate("text",x=12.5, y=23, hjust=1, label="[Q2]", size=6, color="black") +
  annotate("text",x=12.5, y=-23, hjust=1, label="[Q3]", size=6, color="black") +
  annotate("text",x=-12.5, y=-23, hjust=0, label="[Q4]", size=6, color="black") +
  ylab("Current account as share of GDP \n") +
  xlab(expression(Delta~"forex / GDP*0.02")) +
  scale_y_continuous(limits=c(-25,25), breaks=seq(-20,20,10)) +
  scale_x_continuous(limits=c(-12.5,12.5), breaks=seq(-10,10,5)) +
  scale_color_manual(values = c(Q1 = 'red', Q2 = 'black'), name="Country-Year Label",labels = c("Manipulator", "Non-manipulator")) +
  scale_shape_manual(values = c(Q1 = 17, Q2 = 19),name="Country-Year Label",labels = c("Manipulator", "Non-manipulator")) +
  scale_alpha_manual(values = c(Q1=.8, Q2 = 0.25), name="Country-Year Label",labels = c("Manipulator", "Non-manipulator")) +
  scale_size_manual(values = c(Q1=4, Q2=4), name="Country-Year Label",labels = c("Manipulator", "Non-manipulator")) +
  theme_bw() +
  theme(axis.title=element_text(size=18),axis.text=element_text(size=14),
        axis.text.x=element_text(size=15,hjust=1),axis.text.y=element_text(size=15,hjust=1),
        legend.title=element_text(size=14), legend.text=element_text(size=14),
        legend.position = c(0.025, .3),legend.justification = c(0, 1),
        legend.background = element_blank(),legend.box.background = element_rect(color = "black"))


# FIGURE 2a. scatter plot of FVA/EXP and EXP/GDP
df.plot$FVA <- df.plot$fva_exp
df.plot$EXP <- df.plot$exp_goods_gdp
ggplot(df.plot, aes(x=EXP, y=FVA, shape=as.factor(plot_dum), color=as.factor(plot_dum), alpha=as.factor(plot_dum), size =as.factor(plot_dum))) +
  geom_point() + 
  ylab("Foreign Value Added as a share of Exports \n") +
  xlab("\n Exports as a share of GDP") +
  scale_y_continuous(limits=c(0,.6), breaks=seq(0,.6,.2)) +
  scale_x_continuous(limits=c(0,.85), breaks=seq(0,.8,.2)) +
  scale_color_manual(values = c("red","black"), breaks =c("1", "0"), name="Country-Year Label",labels = c("Manipulator", "Non-manipulator")) +
  scale_shape_manual(values = c(17, 19), breaks =c("1", "0"), name="Country-Year Label",labels = c("Manipulator", "Non-manipulator")) +
  scale_alpha_manual(values = c(.8, .25), breaks =c("1", "0"), name="Country-Year Label",labels = c("Manipulator", "Non-manipulator")) +
  scale_size_manual(values = c(4, 4), breaks =c("1", "0"), name="Country-Year Label",labels = c("Manipulator", "Non-manipulator")) +
  theme_bw() +
  theme(axis.title=element_text(size=22),axis.text=element_text(size=20),
        axis.text.x=element_text(size=22,hjust=1),axis.text.y=element_text(size=20,hjust=1),
        legend.title=element_text(size=18), legend.text=element_text(size=20),
        legend.position = c(0.65, 0.2),legend.justification = c(0, 1),
        legend.background = element_blank(),legend.box.background = element_rect(color = "black"))

# FIGURE 2b. scatter plot of DVX/EXP and EXP/GDP
df.plot$DVX <- df.plot$dvx_exp
df.plot$EXP <- df.plot$exp_goods_gdp
ggplot(df.plot, aes(x=EXP, y=DVX, shape=as.factor(plot_dum), color=as.factor(plot_dum), alpha=as.factor(plot_dum), size =as.factor(plot_dum))) +
  geom_point() + 
  ylab("Indirect Value Added as a share of Exports \n") +
  xlab("\n Exports as a share of GDP") +
  scale_y_continuous(limits=c(0,.6), breaks=seq(0,.6,.2)) +
  scale_x_continuous(limits=c(0,.85), breaks=seq(0,.8,.2)) +
  scale_color_manual(values = c("red","black"), breaks =c("1", "0"), name="Country-Year Label",labels = c("Manipulator", "Non-manipulator")) +
  scale_shape_manual(values = c(17, 19), breaks =c("1", "0"), name="Country-Year Label",labels = c("Manipulator", "Non-manipulator")) +
  scale_alpha_manual(values = c(.8, .25), breaks =c("1", "0"), name="Country-Year Label",labels = c("Manipulator", "Non-manipulator")) +
  scale_size_manual(values = c(4, 4), breaks =c("1", "0"), name="Country-Year Label",labels = c("Manipulator", "Non-manipulator")) +
  theme_bw() +
  theme(axis.title=element_text(size=22),axis.text=element_text(size=20),
        axis.text.x=element_text(size=22,hjust=1),axis.text.y=element_text(size=20,hjust=1),
        legend.title=element_text(size=18), legend.text=element_text(size=20),
        legend.position = c(0.65, 0.2),legend.justification = c(0, 1),
        legend.background = element_blank(),legend.box.background = element_rect(color = "black"))


#-----------------------------------------------------------------------------------------------------------------------------------------------------------------
# 3. MODEL 1: RESTRICTED SAMPLE, BALANCED PANEL AND NO EA12/19 COUNTRIES, MANUFACTURING-EXPORTERS ONLY, 2000-2018
#             Outcome var: ln(forex/gdp)_t+1
#             No. countries: 64 (63 with controls)
#-----------------------------------------------------------------------------------------------------------------------------------------------------------------

df.sub$Y   <- df.sub$l_forex_gdp_f1 # define outcome variable (choices: l_forex_f1, l_forex_gdp_f1, forexgdp_f1)

# model 1
mod1a.sub      <- felm(Y ~ EXP    | year_factor + country_factor | 0 | country_factor, data=df.sub)
mod1b.sub      <- felm(Y ~ GPN*EXP | year_factor + country_factor | 0 | country_factor, data=df.sub)
mod1c.sub      <- felm(Y ~ FVA*EXP | year_factor + country_factor | 0 | country_factor, data=df.sub)
mod1d.sub      <- felm(Y ~ DVX*EXP | year_factor + country_factor | 0 | country_factor, data=df.sub)
mod1a.ctrl.sub <- felm(Y ~ EXP    + l_fdi_out_gdp + polity2 + domsave_gdp + ckaopen2010 | year_factor + country_factor | 0 | country_factor, data=df.sub)
mod1b.ctrl.sub <- felm(Y ~ GPN*EXP + l_fdi_out_gdp + polity2 + domsave_gdp + ckaopen2010 | year_factor + country_factor | 0 | country_factor, data=df.sub)
mod1c.ctrl.sub <- felm(Y ~ FVA*EXP + l_fdi_out_gdp + polity2 + domsave_gdp + ckaopen2010 | year_factor + country_factor | 0 | country_factor, data=df.sub)
mod1d.ctrl.sub <- felm(Y ~ DVX*EXP + l_fdi_out_gdp + polity2 + domsave_gdp + ckaopen2010 | year_factor + country_factor | 0 | country_factor, data=df.sub)

texreg(list(mod1a.sub,mod1b.sub,mod1c.sub,mod1d.sub,mod1a.ctrl.sub,mod1b.ctrl.sub,mod1c.ctrl.sub,mod1d.ctrl.sub))

#--------------------------------------------------------------------------------------------------------------------------------------
# Marginal effects plots using binning estimates from Hainmueller et al. (2018):
# Binning estimates, with country/year fixed effects, CSE(country):

# FIGURE 7a.int: X = GPN
df.sub$Y <- df.sub$l_forex_gdp_f1
inter.binning(Y="Y", X="GPN", D="EXP", Z=c("l_fdi_out_gdp","polity2","domsave_gdp","ckaopen2010"), data = df.sub, 
              Xdistr = "density", Ylabel="ln(forex/gdp)", Dlabel="ln(exp/gdp)", Xlabel="ln(gpn/exp)", na.rm=TRUE, 
              FE=c("country","year"), cl="country", theme.bw = T, cex.axis = 1.3, cex.lab = 1.3)

# FIGURE 8a.int: X = FVA
df.sub$Y <- df.sub$l_forex_gdp_f1
inter.binning(Y="Y", X="FVA", D="EXP", Z=c("l_fdi_out_gdp","polity2","domsave_gdp","ckaopen2010"), data = df.sub, 
              Xdistr = "density", Ylabel="ln(forex/gdp)", Dlabel="ln(exp/gdp)", Xlabel="ln(fva/exp)", na.rm=TRUE, 
              FE=c("country","year"), cl="country", theme.bw = T, cex.axis = 1.3, cex.lab = 1.3)

# FIGURE 9a.int: X = DVX
df.sub$Y <- df.sub$l_forex_gdp_f1
inter.binning(Y="Y", X="DVX", D="EXP", Z=c("l_fdi_out_gdp","polity2","domsave_gdp","ckaopen2010"), data = df.sub, 
              Xdistr = "density", Ylabel="ln(forex/gdp)", Dlabel="ln(exp/gdp)", Xlabel="ln(dvx/exp)", na.rm=TRUE, 
              FE=c("country","year"), cl="country", theme.bw = T, cex.axis = 1.3, cex.lab = 1.3)


#-----------------------------------------------------------------------------------------------------------------------------------------------------------------
# 4. MODEL 2: RESTRICTED SAMPLE, BALANCED PANEL AND NO EA12/19 COUNTRIES, MANUFACTURING-EXPORTERS ONLY, 2000-2018
#             Outcome var: [(forex-forex_t-1)/(GDP*.02)]_t+1 -1  
#             No. countries: 64 (70 with controls)
#-----------------------------------------------------------------------------------------------------------------------------------------------------------------

df.sub$Y   <- df.sub$forex_2pctgdp_f1 #forex_2pctgdp_f1, l_forex2pct_f1

# model 2:
mod2a.sub      <- felm(Y ~ EXP    | year_factor + country_factor | 0 | country_factor, data=df.sub)
mod2b.sub      <- felm(Y ~ GPN*EXP | year_factor + country_factor | 0 | country_factor, data=df.sub)
mod2c.sub      <- felm(Y ~ FVA*EXP | year_factor + country_factor | 0 | country_factor, data=df.sub)
mod2d.sub      <- felm(Y ~ DVX*EXP | year_factor + country_factor | 0 | country_factor, data=df.sub)
mod2a.ctrl.sub <- felm(Y ~ EXP    + l_fdi_out_gdp + polity2 + domsave_gdp + ckaopen2010 | year_factor + country_factor | 0 | country_factor, data=df.sub)
mod2b.ctrl.sub <- felm(Y ~ GPN*EXP + l_fdi_out_gdp + polity2 + domsave_gdp + ckaopen2010 | year_factor + country_factor | 0 | country_factor, data=df.sub)
mod2c.ctrl.sub <- felm(Y ~ FVA*EXP + l_fdi_out_gdp + polity2 + domsave_gdp + ckaopen2010 | year_factor + country_factor | 0 | country_factor, data=df.sub)
mod2d.ctrl.sub <- felm(Y ~ DVX*EXP + l_fdi_out_gdp + polity2 + domsave_gdp + ckaopen2010 | year_factor + country_factor | 0 | country_factor, data=df.sub)

texreg(list(mod2a.sub,mod2b.sub,mod2c.sub,mod2d.sub,mod2a.ctrl.sub,mod2b.ctrl.sub,mod2c.ctrl.sub,mod2d.ctrl.sub))

#--------------------------------------------------------------------------------------------------------------------------------------
# Marginal effects plots using binning estimates from Hainmueller et al. (2018):
# Binning estimates, with country/year fixed effects, CSE(country):

# FIGURE 4a: X = GPN
df.sub$FX <- df.sub$forex_2pctgdp_f1
inter.binning(Y="Y", X="GPN", D="EXP", Z=c("l_fdi_out_gdp","polity2","domsave_gdp","ckaopen2010"), data = df.sub, 
              Xdistr = "density", Ylabel="forex_intensity", Dlabel="ln(exp/gdp)", Xlabel="ln(gpn/exp)", na.rm=TRUE, 
              FE=c("country","year"), cl="country", theme.bw = T, cex.axis = 1.3, cex.lab = 1.3)

# FIGURE 4b: X = FVA
df.sub$FX <- df.sub$forex_2pctgdp_f1
inter.binning(Y="Y", X="FVA", D="EXP", Z=c("l_fdi_out_gdp","polity2","domsave_gdp","ckaopen2010"), data = df.sub, 
              Xdistr = "density", Ylabel="forex_intensity", Dlabel="ln(exp/gdp)", Xlabel="ln(fva/exp)", na.rm=TRUE, 
              FE=c("country","year"), cl="country", theme.bw = T, cex.axis = 1.3, cex.lab = 1.3)

# FIGURE 4c: X = DVX
df.sub$FX <- df.sub$forex_2pctgdp_f1
inter.binning(Y="Y", X="DVX", D="EXP", Z=c("l_fdi_out_gdp","polity2","domsave_gdp","ckaopen2010"), data = df.sub, 
              Xdistr = "density", Ylabel="forex_intensity", Dlabel="ln(exp/gdp)", Xlabel="ln(dvx/exp)", na.rm=TRUE, 
              FE=c("country","year"), cl="country", theme.bw = T, cex.axis = 1.3, cex.lab = 1.3)
    


#-----------------------------------------------------------------------------------------------------------------------------------------------------------------
# 5. MODEL 3: RESTRICTED SAMPLE, BALANCED PANEL AND NO EA12/19 COUNTRIES: ERER-REER = GVC*EXP/GDP
#             Outcome var: ERER-REER (mis_broad)_t+1
#             No. countries: 68 (68 with controls)
#-----------------------------------------------------------------------------------------------------------------------------------------------------------------

df.sub$Y   <- df.sub$mis_n_tv_f1 # define the xr value variable (choices: mis_n_tv_f1  mis_b_tv_f1 mis_n_fw_f1  mis_b_fw_f1 mis_broad_f1 pl_gdp_hat_f1 pl_gdp_hat2_f1)

# model 3:
mod3a.sub      <- felm(Y ~ EXP     | year_factor + country_factor | 0 | country_factor, data=df.sub)
mod3b.sub      <- felm(Y ~ GPN*EXP | year_factor + country_factor | 0 | country_factor, data=df.sub)
mod3c.sub      <- felm(Y ~ FVA*EXP | year_factor + country_factor | 0 | country_factor, data=df.sub)
mod3d.sub      <- felm(Y ~ DVX*EXP | year_factor + country_factor | 0 | country_factor, data=df.sub)
mod3a.ctrl.sub <- felm(Y ~ EXP     + l_fdi_out_gdp + polity2 + domsave_gdp + ckaopen2010 | year_factor + country_factor | 0 | country_factor, data=df.sub)
mod3b.ctrl.sub <- felm(Y ~ GPN*EXP + l_fdi_out_gdp + polity2 + domsave_gdp + ckaopen2010 | year_factor + country_factor | 0 | country_factor, data=df.sub)
mod3c.ctrl.sub <- felm(Y ~ FVA*EXP + l_fdi_out_gdp + polity2 + domsave_gdp + ckaopen2010 | year_factor + country_factor | 0 | country_factor, data=df.sub)
mod3d.ctrl.sub <- felm(Y ~ DVX*EXP + l_fdi_out_gdp + polity2 + domsave_gdp + ckaopen2010 | year_factor + country_factor | 0 | country_factor, data=df.sub)

texreg(list(mod3a.sub,mod3b.sub,mod3c.sub,mod3d.sub,mod3a.ctrl.sub,mod3b.ctrl.sub,mod3c.ctrl.sub,mod3d.ctrl.sub))

#--------------------------------------------------------------------------------------------------------------------------------------
# Marginal effects plots using binning estimates from Hainmueller et al. (2018):
# Binning estimates, with country/year fixed effects, CSE(country):

# FIGURE 5a: X = GPN
df.sub$XR <- df.sub$mis_n_tv_f1
inter.binning(Y="Y", X="GPN", D="EXP", Z=c("l_fdi_out_gdp","polity2","domsave_gdp","ckaopen2010"), data = df.sub, 
              Xdistr = "density", Ylabel="xr_hat", Dlabel="ln(exp/gdp)", Xlabel="ln(gpn/exp)", na.rm=TRUE, 
              FE=c("country","year"), cl="country", theme.bw = T, cex.axis = 1.3, cex.lab = 1.3 )

# FIGURE 5b: X = FVA
df.sub$XR <- df.sub$mis_n_tv_f1
inter.binning(Y="Y", X="FVA", D="EXP", Z=c("l_fdi_out_gdp","polity2","domsave_gdp","ckaopen2010"), data = df.sub, 
              Xdistr = "density", Ylabel="xr_hat", Dlabel="ln(exp/gdp)", Xlabel="ln(fva/exp)", na.rm=TRUE, 
              FE=c("country","year"), cl="country", theme.bw = T, cex.axis = 1.3, cex.lab = 1.3 )

# FIGURE 5c: X = DVX
df.sub$XR <- df.sub$mis_n_tv_f1
inter.binning(Y="Y", X="DVX", D="EXP", Z=c("l_fdi_out_gdp","polity2","domsave_gdp","ckaopen2010"), data = df.sub, 
              Xdistr = "density", Ylabel="xr_hat", Dlabel="ln(exp/gdp)", Xlabel="ln(dvx/exp)", na.rm=TRUE, 
              FE=c("country","year"), cl="country", theme.bw = T, cex.axis = 1.3, cex.lab = 1.3 )


#-----------------------------------------------------------------------------------------------------------------------------------------------------------------
# 6. COEFFICIENT PLOTS (ALL MODELS), BY OUTCOME VARIABLE
#-----------------------------------------------------------------------------------------------------------------------------------------------------------------

#--------------------------------------------
# 6.A.     EXPORTS AS MAIN PREDICTOR
#--------------------------------------------

#6.A.1 coef plot of mod1a.ctrl.sub  (figure 6a)
dwplot(tidy(mod1a.ctrl.sub), whisker_args = list(alpha = 1, size = 1.5), dot_args = list(alpha = 1, size = 3, shape=19), 
  vline = geom_vline(xintercept = 0, colour = "grey60", linetype = 2)) %>% # plot line at zero _behind_ coefs
  relabel_predictors(c(EXP = "exp/gdp",                       
                       l_fdi_out_gdp = "fdi_out/gdp", 
                       polity2 = "polity", 
                       domsave_gdp = "save/gdp", 
                       ckaopen2010 = "ckaopen")) +
  scale_color_grey() + scale_x_continuous(limits=c(-1,1), breaks=seq(-1,1,.5)) +
  theme_bw() + xlab(" ") + ylab("") +
  geom_vline(xintercept = 0, colour = "grey60", linetype = 2) +
  facet_grid(. ~ "Y = forex/exp (t+1)") +
  theme(strip.background = element_rect(fill="lightgrey"),
        strip.text = element_text(face="bold", size=20),
        axis.title=element_text(size=18),axis.text=element_text(size=16),
        axis.text.x=element_text(size=16,hjust=.5),axis.text.y=element_text(size=20,hjust=1),
        legend.position = "none", panel.grid.minor = element_blank()) 

#6.A.2 coef plot of mod2a.ctrl.sub (figure 6b)
dwplot(tidy(mod2a.ctrl.sub), whisker_args = list(alpha = 1, size = 1.5), dot_args = list(alpha = 1, size = 3, shape=19), 
       vline = geom_vline(xintercept = 0, colour = "grey60", linetype = 2)) %>% # plot line at zero _behind_ coefs
  relabel_predictors(c(EXP = "exp/gdp",                       
                       l_fdi_out_gdp = "fdi_out/gdp", 
                       polity2 = "polity", 
                       domsave_gdp = "save/gdp", 
                       ckaopen2010 = "ckaopen")) +
  scale_color_grey() + scale_x_continuous(limits=c(-1,1), breaks=seq(-1,1,.5)) +
  theme_bw() + xlab(" ") + ylab("") +
  geom_vline(xintercept = 0, colour = "grey60", linetype = 2) +
  facet_grid(. ~ "Y = forex_intensity (t+1)") +
  theme(strip.background = element_rect(fill="lightgrey"),
        strip.text = element_text(face="bold", size=20),
        axis.title=element_text(size=18),axis.text=element_text(size=16),
        axis.text.x=element_text(size=16,hjust=.5),axis.text.y=element_text(size=20,hjust=1),
        legend.position = "none", panel.grid.minor = element_blank()) 


#6.A.3 coef plot of mod3a.ctrl.sub (figure 6c)
dwplot(tidy(mod3a.ctrl.sub), whisker_args = list(alpha = 1, size = 1.5), dot_args = list(alpha = 1, size = 3, shape=19), 
       vline = geom_vline(xintercept = 0, colour = "grey60", linetype = 2)) %>% # plot line at zero _behind_ coefs
  relabel_predictors(c(EXP = "exp/gdp",                       
                       l_fdi_out_gdp = "fdi_out/gdp", 
                       polity2 = "polity", 
                       domsave_gdp = "save/gdp", 
                       ckaopen2010 = "ckaopen")) +
  scale_color_grey() + scale_x_continuous(limits=c(-1,1), breaks=seq(-1,1,.5)) +
  theme_bw() + xlab(" ") + ylab("") +
  geom_vline(xintercept = 0, colour = "grey60", linetype = 2) +
  facet_grid(. ~ "Y = xr_hat (t+1)") +
  theme(strip.background = element_rect(fill="lightgrey"),
        strip.text = element_text(face="bold", size=20),
        axis.title=element_text(size=18),axis.text=element_text(size=16),
        axis.text.x=element_text(size=16,hjust=.5),axis.text.y=element_text(size=20,hjust=1),
        legend.position = "none", panel.grid.minor = element_blank()) 

#--------------------------------------------
# 6.B. EXPORTS X GPN
#--------------------------------------------

#6.B.1 coef plot of mod1b.ctrl.sub  (figure 7a)
dwplot(tidy(mod1b.ctrl.sub), whisker_args = list(alpha = 1, size = 1.5), dot_args = list(alpha = 1, size = 3, shape=19), 
       vline = geom_vline(xintercept = 0, colour = "grey60", linetype = 2)) %>% # plot line at zero _behind_ coefs
  relabel_predictors(c(EXP = "exp/gdp", 
                       GPN = "gpn/exp", 
                       "GPN:EXP" = "(gpn/exp)*(exp/gdp)", 
                       l_fdi_out_gdp = "fdi_out/gdp", 
                       polity2 = "polity", 
                       domsave_gdp = "save/gdp", 
                       ckaopen2010 = "ckaopen")) +
  scale_color_grey() + scale_x_continuous(limits=c(-5.1,2.25), breaks=seq(-5,2,1)) +
  theme_bw() + xlab(" ") + ylab("") +
  geom_vline(xintercept = 0, colour = "grey60", linetype = 2) +
  facet_grid(. ~ "Y = forex/exp (t+1)") +
  theme(strip.background = element_rect(fill="lightgrey"),
        strip.text = element_text(face="bold", size=20),
        axis.title=element_text(size=18),axis.text=element_text(size=16),
        axis.text.x=element_text(size=16,hjust=.5),axis.text.y=element_text(size=20,hjust=1),
        legend.position = "none", panel.grid.minor = element_blank()) 

#6.B.2 coef plot of mod2b.ctrl.sub  (figure 7b)
dwplot(tidy(mod2b.ctrl.sub), whisker_args = list(alpha = 1, size = 1.5), dot_args = list(alpha = 1, size = 3, shape=19), 
       vline = geom_vline(xintercept = 0, colour = "grey60", linetype = 2)) %>% # plot line at zero _behind_ coefs
  relabel_predictors(c(EXP = "exp/gdp", 
                       GPN = "gpn/exp", 
                       "GPN:EXP" = "(gpn/exp)*(exp/gdp)", 
                       l_fdi_out_gdp = "fdi_out/gdp", 
                       polity2 = "polity", 
                       domsave_gdp = "save/gdp", 
                       ckaopen2010 = "ckaopen")) +
  scale_color_grey() + scale_x_continuous(limits=c(-5.1,2.25), breaks=seq(-5,2,1)) +
  theme_bw() + xlab(" ") + ylab("") +
  geom_vline(xintercept = 0, colour = "grey60", linetype = 2) +
  facet_grid(. ~ "Y = forex_intensity (t+1)") +
  theme(strip.background = element_rect(fill="lightgrey"),
        strip.text = element_text(face="bold", size=20),
        axis.title=element_text(size=18),axis.text=element_text(size=16),
        axis.text.x=element_text(size=16,hjust=.5),axis.text.y=element_text(size=20,hjust=1),
        legend.position = "none", panel.grid.minor = element_blank()) 

#6.B.3  coef plot of mod3b.ctrl.sub  (figure 7c)
dwplot(tidy(mod3b.ctrl.sub), whisker_args = list(alpha = 1, size = 1.5), dot_args = list(alpha = 1, size = 3, shape=19), 
       vline = geom_vline(xintercept = 0, colour = "grey60", linetype = 2)) %>% # plot line at zero _behind_ coefs
  relabel_predictors(c(EXP = "exp/gdp", 
                       GPN = "gpn/exp", 
                       "GPN:EXP" = "(gpn/exp)*(exp/gdp)", 
                       l_fdi_out_gdp = "fdi_out/gdp", 
                       polity2 = "polity", 
                       domsave_gdp = "save/gdp", 
                       ckaopen2010 = "ckaopen")) +
  scale_color_grey() + scale_x_continuous(limits=c(-5.1,2.25), breaks=seq(-5,2,1)) +
  theme_bw() + xlab(" ") + ylab("") +
  geom_vline(xintercept = 0, colour = "grey60", linetype = 2) +
  facet_grid(. ~ "Y = xr_hat (t+1)") +
  theme(strip.background = element_rect(fill="lightgrey"),
        strip.text = element_text(face="bold", size=20),
        axis.title=element_text(size=18),axis.text=element_text(size=16),
        axis.text.x=element_text(size=16,hjust=.5),axis.text.y=element_text(size=20,hjust=1),
        legend.position = "none", panel.grid.minor = element_blank()) 

#--------------------------------------------
# 6.C EXPORTS X FVA
#--------------------------------------------

#6.C.1  coef plot of mod1c.ctrl.sub (figure 8a)
dwplot(tidy(mod1c.ctrl.sub), whisker_args = list(alpha = 1, size = 1.5), dot_args = list(alpha = 1, size = 3, shape=19), 
       vline = geom_vline(xintercept = 0, colour = "grey60", linetype = 2)) %>% # plot line at zero _behind_ coefs
  relabel_predictors(c(EXP = "exp/gdp", 
                       FVA = "fva/exp", 
                       "FVA:EXP" = "(fva/exp)*(exp/gdp)", 
                       l_fdi_out_gdp = "fdi_out/gdp", 
                       polity2 = "polity", 
                       domsave_gdp = "save/gdp", 
                       ckaopen2010 = "ckaopen")) +
  scale_color_grey() + scale_x_continuous(limits=c(-2.5,2), breaks=seq(-2,2,1)) +
  theme_bw() + xlab(" ") + ylab("") +
  geom_vline(xintercept = 0, colour = "grey60", linetype = 2) +
  facet_grid(. ~ "Y = forex/exp (t+1)") +
  theme(strip.background = element_rect(fill="lightgrey"),
        strip.text = element_text(face="bold", size=20),
        axis.title=element_text(size=18),axis.text=element_text(size=16),
        axis.text.x=element_text(size=16,hjust=.5),axis.text.y=element_text(size=20,hjust=1),
        legend.position = "none", panel.grid.minor = element_blank()) 

#6.C.2 coef plot of mod2c.ctrl.sub (figure 8b)
dwplot(tidy(mod2c.ctrl.sub), whisker_args = list(alpha = 1, size = 1.5), dot_args = list(alpha = 1, size = 3, shape=19), 
       vline = geom_vline(xintercept = 0, colour = "grey60", linetype = 2)) %>% # plot line at zero _behind_ coefs
  relabel_predictors(c(EXP = "exp/gdp", 
                       FVA = "fva/exp", 
                       "FVA:EXP" = "(fva/exp)*(exp/gdp)", 
                       l_fdi_out_gdp = "fdi_out/gdp", 
                       polity2 = "polity", 
                       domsave_gdp = "save/gdp", 
                       ckaopen2010 = "ckaopen")) +
  scale_color_grey() + scale_x_continuous(limits=c(-2.5,2), breaks=seq(-2,2,1)) +
  theme_bw() + xlab(" ") + ylab("") +
  geom_vline(xintercept = 0, colour = "grey60", linetype = 2) +
  facet_grid(. ~ "Y = forex_intensity (t+1)") +
  theme(strip.background = element_rect(fill="lightgrey"),
        strip.text = element_text(face="bold", size=20),
        axis.title=element_text(size=18),axis.text=element_text(size=16),
        axis.text.x=element_text(size=16,hjust=.5),axis.text.y=element_text(size=20,hjust=1),
        legend.position = "none", panel.grid.minor = element_blank()) 

#6.C.3 coef plot of mod3c.ctrl.sub (figure 8c)
dwplot(tidy(mod3c.ctrl.sub),  whisker_args = list(alpha = 1, size = 1.5), dot_args = list(alpha = 1, size = 3, shape=19), 
       vline = geom_vline(xintercept = 0, colour = "grey60", linetype = 2)) %>% # plot line at zero _behind_ coefs
  relabel_predictors(c(EXP = "exp/gdp", 
                       FVA = "fva/exp", 
                       "FVA:EXP" = "(fva/exp)*(exp/gdp)", 
                       l_fdi_out_gdp = "fdi_out/gdp", 
                       polity2 = "polity", 
                       domsave_gdp = "save/gdp", 
                       ckaopen2010 = "ckaopen")) +
  scale_color_grey() + scale_x_continuous(limits=c(-2.5,2), breaks=seq(-2,2,1)) +
  theme_bw() + xlab(" ") + ylab("") +
  geom_vline(xintercept = 0, colour = "grey60", linetype = 2) +
  facet_grid(. ~ "Y = xr_hat (t+1)") +
  theme(strip.background = element_rect(fill="lightgrey"),
        strip.text = element_text(face="bold", size=20),
        axis.title=element_text(size=18),axis.text=element_text(size=16),
        axis.text.x=element_text(size=16,hjust=.5),axis.text.y=element_text(size=20,hjust=1),
        legend.position = "none", panel.grid.minor = element_blank()) 

#--------------------------------------------
# 6.D EXPORTS X DVX
#--------------------------------------------

#6.D.1 coef plot of mod1d.ctrl.sub (figure 9a)
dwplot(tidy(mod1d.ctrl.sub), whisker_args = list(alpha = 1, size = 1.5), dot_args = list(alpha = 1, size = 3, shape=19), 
       vline = geom_vline(xintercept = 0, colour = "grey60", linetype = 2)) %>% # plot line at zero _behind_ coefs
  relabel_predictors(c(EXP = "exp/gdp", 
                       DVX = "dvx/exp", 
                       "DVX:EXP" = "(dvx/exp)*(exp/gdp)", 
                       l_fdi_out_gdp = "fdi_out/gdp", 
                       polity2 = "polity", 
                       domsave_gdp = "save/gdp", 
                       ckaopen2010 = "ckaopen")) +
  scale_color_grey() +
  scale_x_continuous(limits=c(-6,1), breaks=seq(-6,1,1)) +
  theme_bw() + xlab(" ") + ylab("") +
  geom_vline(xintercept = 0, colour = "grey60", linetype = 2) +
  facet_grid(. ~ "Y = forex/exp (t+1)") +
  theme(strip.background = element_rect(fill="lightgrey"),
        strip.text = element_text(face="bold", size=20),
        axis.title=element_text(size=18),axis.text=element_text(size=16),
        axis.text.x=element_text(size=16,hjust=.5),axis.text.y=element_text(size=20,hjust=1),
        legend.position = "none", panel.grid.minor = element_blank()) 

#6.D.2 coef plot of mod2d.ctrl.sub  (figure 9b)
dwplot(tidy(mod2d.ctrl.sub), whisker_args = list(alpha = 1, size = 1.5), dot_args = list(alpha = 1, size = 3, shape=19), 
       vline = geom_vline(xintercept = 0, colour = "grey60", linetype = 2)) %>% # plot line at zero _behind_ coefs
  relabel_predictors(c(EXP = "exp/gdp", 
                       DVX = "dvx/exp", 
                       "DVX:EXP" = "(dvx/exp)*(exp/gdp)", 
                       l_fdi_out_gdp = "fdi_out/gdp", 
                       polity2 = "polity", 
                       domsave_gdp = "save/gdp", 
                       ckaopen2010 = "ckaopen")) +
  scale_color_grey() +
  scale_x_continuous(limits=c(-6,1), breaks=seq(-6,1,1)) +
  theme_bw() + xlab(" ") + ylab("") +
  geom_vline(xintercept = 0, colour = "grey60", linetype = 2) +
  facet_grid(. ~ "Y = forex_intensity (t+1)") +
  theme(strip.background = element_rect(fill="lightgrey"),
        strip.text = element_text(face="bold", size=20),
        axis.title=element_text(size=18),axis.text=element_text(size=16),
        axis.text.x=element_text(size=16,hjust=.5),axis.text.y=element_text(size=20,hjust=1),
        legend.position = "none", panel.grid.minor = element_blank()) 

#6.D.3 coef plot of mod3d.ctrl.sub (figure 9c)
dwplot(tidy(mod3d.ctrl.sub), whisker_args = list(alpha = 1, size = 1.5), dot_args = list(alpha = 1, size = 3, shape=19), 
       vline = geom_vline(xintercept = 0, colour = "grey60", linetype = 2)) %>% # plot line at zero _behind_ coefs
  relabel_predictors(c(EXP = "exp/gdp", 
                       DVX = "dvx/exp", 
                       "DVX:EXP" = "(dvx/exp)*(exp/gdp)", 
                       l_fdi_out_gdp = "fdi_out/gdp", 
                       polity2 = "polity", 
                       domsave_gdp = "save/gdp", 
                       ckaopen2010 = "ckaopen")) +
  scale_color_grey() +
  scale_x_continuous(limits=c(-6,1), breaks=seq(-6,1,1)) +
  theme_bw() + xlab(" ") + ylab("") +
  geom_vline(xintercept = 0, colour = "grey60", linetype = 2) +
  facet_grid(. ~ "Y = xr_hat (t+1)") +
  theme(strip.background = element_rect(fill="lightgrey"),
        strip.text = element_text(face="bold", size=20),
        axis.title=element_text(size=18),axis.text=element_text(size=16),
        axis.text.x=element_text(size=16,hjust=.5),axis.text.y=element_text(size=20,hjust=1),
        legend.position = "none", panel.grid.minor = element_blank()) 



#-----------------------------------------------------------------------------------------------------------------------------------------------------------------
# 7. MODEL 1: FULL SAMPLE, 2000-2018
#             Outcome var: ln(forex/gdp)_t+1
#             No. countries: 154
#-----------------------------------------------------------------------------------------------------------------------------------------------------------------

df.full <- d[which(d$year>=2000),]

# factors, assign variables:
df.full$year_factor    <- as.factor(df.full$year) # for year fixed effects
df.full$country_factor <- as.factor(df.full$country_cd) # for country fixed effects
df.full$Y   <- df.full$l_forex_gdp_f1 # define outcome variable (choices: l_forex_f1, l_forex_gdp_f1, forexgdp_f1)
df.full$GPN <- df.full$l_gvc_exp
df.full$FVA <- df.full$l_fva_exp
df.full$DVX <- df.full$l_dvx_exp
df.full$EXP <- df.full$l_exp_goods_gdp
df.full     <- subset(df.full, !is.na(EXP)) 

# model 1 (full sample)
mod1a.full      <- felm(Y ~ EXP    | year_factor + country_factor | 0 | country_factor, data=df.full)
mod1b.full      <- felm(Y ~ GPN*EXP | year_factor + country_factor | 0 | country_factor, data=df.full)
mod1c.full      <- felm(Y ~ FVA*EXP | year_factor + country_factor | 0 | country_factor, data=df.full)
mod1d.full      <- felm(Y ~ DVX*EXP | year_factor + country_factor | 0 | country_factor, data=df.full)
mod1a.ctrl.full <- felm(Y ~ EXP    + l_fdi_out_gdp + polity2 + domsave_gdp + ckaopen2010 | year_factor + country_factor | 0 | country_factor, data=df.full)
mod1b.ctrl.full <- felm(Y ~ GPN*EXP + l_fdi_out_gdp + polity2 + domsave_gdp + ckaopen2010 | year_factor + country_factor | 0 | country_factor, data=df.full)
mod1c.ctrl.full <- felm(Y ~ FVA*EXP + l_fdi_out_gdp + polity2 + domsave_gdp + ckaopen2010 | year_factor + country_factor | 0 | country_factor, data=df.full)
mod1d.ctrl.full <- felm(Y ~ DVX*EXP + l_fdi_out_gdp + polity2 + domsave_gdp + ckaopen2010 | year_factor + country_factor | 0 | country_factor, data=df.full)

texreg(list(mod1a.full,mod1b.full,mod1c.full,mod1d.full,mod1a.ctrl.full,mod1b.ctrl.full,mod1c.ctrl.full,mod1d.ctrl.full))

#-----------------------------------------------------------------------------------------------------------------------------------------------------------------
# 8. MODEL 2: FULL SAMPLE, 2000-2018
#             Outcome var: [(forex-forex_t-1)/(GDP*.02)]_t+1 -1  
#             No. countries: 154 (120 with controls)
#-----------------------------------------------------------------------------------------------------------------------------------------------------------------

df.full <- d[which(d$year>=2000),]

# factors, assign variables:
df.full$year_factor    <- as.factor(df.full$year) # for year fixed effects
df.full$country_factor <- as.factor(df.full$country_cd) # for country fixed effects
df.full$Y   <- df.full$forex_2pctgdp_f1 #forex_2pctgdp_f1, l_forex2pct_f1
df.full$GPN <- df.full$l_gvc_exp
df.full$FVA <- df.full$l_fva_exp
df.full$DVX <- df.full$l_dvx_exp
df.full$EXP <- df.full$l_exp_goods_gdp
df.full     <- subset(df.full, !is.na(EXP)) 

# model 2:
mod2a.full      <- felm(Y ~ EXP    | year_factor + country_factor | 0 | country_factor, data=df.full)
mod2b.full      <- felm(Y ~ GPN*EXP | year_factor + country_factor | 0 | country_factor, data=df.full)
mod2c.full      <- felm(Y ~ FVA*EXP | year_factor + country_factor | 0 | country_factor, data=df.full)
mod2d.full      <- felm(Y ~ DVX*EXP | year_factor + country_factor | 0 | country_factor, data=df.full)
mod2a.ctrl.full <- felm(Y ~ EXP    + l_fdi_out_gdp + polity2 + domsave_gdp + ckaopen2010 | year_factor + country_factor | 0 | country_factor, data=df.full)
mod2b.ctrl.full <- felm(Y ~ GPN*EXP + l_fdi_out_gdp + polity2 + domsave_gdp + ckaopen2010 | year_factor + country_factor | 0 | country_factor, data=df.full)
mod2c.ctrl.full <- felm(Y ~ FVA*EXP + l_fdi_out_gdp + polity2 + domsave_gdp + ckaopen2010 | year_factor + country_factor | 0 | country_factor, data=df.full)
mod2d.ctrl.full <- felm(Y ~ DVX*EXP + l_fdi_out_gdp + polity2 + domsave_gdp + ckaopen2010 | year_factor + country_factor | 0 | country_factor, data=df.full)

texreg(list(mod2a.full,mod2b.full,mod2c.full,mod2d.full,mod2a.ctrl.full,mod2b.ctrl.full,mod2c.ctrl.full,mod2d.ctrl.full))

#-----------------------------------------------------------------------------------------------------------------------------------------------------------------
# 9. MODEL 3: FULL SAMPLE, 2000-2018
#             Outcome var: ERER-REER (mis_broad)_t+1
#             No. countries: 154 (120 with controls)
#-----------------------------------------------------------------------------------------------------------------------------------------------------------------

df.full <- d[which(d$year>=2000),]

# factors, assign variables:
df.full$year_factor    <- as.factor(df.full$year) # for year fixed effects
df.full$country_factor <- as.factor(df.full$country_cd) # for country fixed effects
df.full$Y   <- df.full$mis_n_tv_f1 # define the xr value variable (choices: mis_n_tv_f1  mis_b_tv_f1 mis_n_fw_f1  mis_b_fw_f1 mis_broad_f1 pl_gdp_hat_f1 pl_gdp_hat2_f1)
df.full$GPN <- df.full$l_gvc_exp
df.full$FVA <- df.full$l_fva_exp
df.full$DVX <- df.full$l_dvx_exp
df.full$EXP <- df.full$l_exp_goods_gdp
df.full     <- subset(df.full, !is.na(EXP)) 

mod3a.full      <- felm(Y ~ EXP     | year_factor + country_factor | 0 | country_factor, data=df.full)
mod3b.full      <- felm(Y ~ GPN*EXP | year_factor + country_factor | 0 | country_factor, data=df.full)
mod3c.full      <- felm(Y ~ FVA*EXP | year_factor + country_factor | 0 | country_factor, data=df.full)
mod3d.full      <- felm(Y ~ DVX*EXP | year_factor + country_factor | 0 | country_factor, data=df.full)
mod3a.ctrl.full <- felm(Y ~ EXP     + l_fdi_out_gdp + polity2 + domsave_gdp + ckaopen2010 | year_factor + country_factor | 0 | country_factor, data=df.full)
mod3b.ctrl.full <- felm(Y ~ GPN*EXP + l_fdi_out_gdp + polity2 + domsave_gdp + ckaopen2010 | year_factor + country_factor | 0 | country_factor, data=df.full)
mod3c.ctrl.full <- felm(Y ~ FVA*EXP + l_fdi_out_gdp + polity2 + domsave_gdp + ckaopen2010 | year_factor + country_factor | 0 | country_factor, data=df.full)
mod3d.ctrl.full <- felm(Y ~ DVX*EXP + l_fdi_out_gdp + polity2 + domsave_gdp + ckaopen2010 | year_factor + country_factor | 0 | country_factor, data=df.full)

texreg(list(mod3a.full,mod3b.full,mod3c.full,mod3d.full,mod3a.ctrl.full,mod3b.ctrl.full,mod3c.ctrl.full,mod3d.ctrl.full))
